import { DeleteDto, IndexDto, ShowDto } from '@libs/common/dto';
import { ApiProperty, PartialType } from '@nestjs/swagger';
import {
  IsEnum,
  IsInt,
  IsNotEmpty,
  IsNumber,
  IsOptional,
  IsPositive,
  IsString,
  IsUrl,
} from 'class-validator';

import { Product, StatusEnum } from '../entities/product.entity';

export class IndexProductDto extends IndexDto<Product> {}

export class ShowProductDto extends ShowDto {}

export class DeleteProductDto extends DeleteDto {}

export class CreateProductDto {
  @ApiProperty({ description: '商品名' })
  @IsString({ message: '商品名必须是字符串' })
  name: string;

  @ApiProperty({ description: '商品描述' })
  @IsString({ message: '商品描述必须是字符串' })
  description: string;

  @ApiProperty({ description: '折扣价' })
  @IsOptional()
  discountPrice?: number;

  @ApiProperty({ description: '原价' })
  @IsInt({ message: '折扣价必须是整数' })
  @IsPositive({ message: '原价必须是正数' })
  originalPrice: number;

  @ApiProperty({ description: '最低起购数量', default: 1 })
  @IsPositive({ message: '最低起购数量必须是正数' })
  @IsNumber({}, { message: '最低起购数量必须是数字' })
  minBuy: number = 1;

  @ApiProperty({ description: '最大限购数量' })
  @IsPositive({ message: '最大限购数量必须是正数' })
  @IsNumber({}, { message: '最大限购数量必须是数字' })
  @IsOptional()
  maxBuy?: number;

  @ApiProperty({ description: '商品库存' })
  @IsNumber({}, { message: '商品库存必须是数字' })
  stock?: number = 0;

  @ApiProperty({ description: '商品图片' })
  @IsString({ message: '商品图片必须是字符串' })
  @IsUrl({}, { message: '商品图片必须是URL' })
  image: string;

  @ApiProperty({ description: '商品状态', default: StatusEnum['上架'] })
  @IsOptional()
  @IsEnum(StatusEnum, { message: '商品状态必须是枚举值' })
  status: StatusEnum = StatusEnum['上架'];
}

export class UpdateProductDto extends PartialType(CreateProductDto) {
  @ApiProperty()
  @IsNotEmpty({ message: 'id 不能为空' })
  id: string;
}

export class BuyProductDto {
  @ApiProperty({ description: '数量' })
  @IsNotEmpty({ message: '数量不能为空' })
  @IsPositive({ message: '数量必须是正数' })
  @IsNumber({}, { message: '数量必须是数字' })
  number?: number;

  @ApiProperty({ description: '商品ID' })
  @IsNotEmpty({ message: '商品ID不能为空' })
  productId: string;
}

export class SwitchProductDto {
  @ApiProperty()
  @IsNotEmpty({ message: 'id 不能为空' })
  id: string;

  @ApiProperty({ description: '状态' })
  @IsNotEmpty({ message: '状态不能为空' })
  @IsEnum(StatusEnum)
  status: StatusEnum;
}

export class ImportProductDto {}

export class ExportProductDto {}
